﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Project01
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
     
    public class MainMenuSystemComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        static ManagerGame manager;
        private Game game;
        // phần hiển thị phụ
        SpriteFont Font;
        //-------------------------------------------

        public enum GameState
        {
            MainMenu,
            Playing,
            Option,
            SoundSetting,
            ControlSetting,
            Credit,
            Exit
        }

        GameState currentGameState=GameState.MainMenu;

        // Mục trong Main Menu
        cButton btnNewGame;
        cButton btnContinue;
        cButton btnOption;
        cButton btnExit;

        // Mục trong Option
        cButton btnSoundSetting;
        cButton btnControl;
        cButton btnCredit;
        cButton btnBackM;
        cButton btnBackO;

        Texture2D ButtonTexture;

        public MainMenuSystemComponent(Game game)
            : base(game)
        {
            this.game = game;
        }

        protected override void LoadContent()
        {
            //Mục trong Main Menu
            btnNewGame = new cButton(Game.Content.Load<Texture2D>("HUD/StartGame"), Game);
            btnNewGame.SetPosition(new Vector2(450, 300));

            btnContinue = new cButton(Game.Content.Load<Texture2D>("HUD/continue"), Game);
            btnContinue.SetPosition(new Vector2(450, 350));

            btnOption = new cButton(Game.Content.Load<Texture2D>("HUD/option"), Game);
            btnOption.SetPosition(new Vector2(450, 400));

            btnExit = new cButton(Game.Content.Load<Texture2D>("HUD/exit"), Game);
            btnExit.SetPosition(new Vector2(450, 450));


            //Mục trong Option
            btnSoundSetting = new cButton(Game.Content.Load<Texture2D>("HUD/Button"), Game);
            btnSoundSetting.SetPosition(new Vector2(400, 300));

            btnControl = new cButton(Game.Content.Load<Texture2D>("HUD/Button"), Game);
            btnControl.SetPosition(new Vector2(400, 350));

            btnCredit = new cButton(Game.Content.Load<Texture2D>("HUD/Button"), Game);
            btnCredit.SetPosition(new Vector2(400, 400));

            btnBackM = new cButton(Game.Content.Load<Texture2D>("HUD/Button"), Game);
            btnBackM.SetPosition(new Vector2(400, 450));

            //Mục khác
            btnBackO = new cButton(Game.Content.Load<Texture2D>("HUD/Button"), Game);
            btnBackO.SetPosition(new Vector2(200, 500));

            Font = Game.Content.Load<SpriteFont>("HUD/Text");
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            base.LoadContent();
        }
        public override void Initialize()
        {

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            
            MouseState mouse = Mouse.GetState();

            switch (currentGameState)
            {
                // Mục trong Main Menu
                case GameState.MainMenu:

                    if (btnNewGame.isClicked == true) currentGameState = GameState.Playing;
                    btnNewGame.Update(mouse);

                    if (btnContinue.isClicked == true) currentGameState = GameState.Playing;
                    btnContinue.Update(mouse);
                    
                    if (btnOption.isClicked == true) currentGameState = GameState.Option;
                    btnOption.Update(mouse);
                    
                    if (btnExit.isClicked == true) currentGameState = GameState.Exit;
                    btnExit.Update(mouse);

                    break;
                case GameState.Playing:

                    if (manager == null)
                    {
                        manager = new ManagerGame(game);
                    }
                    manager.Update(gameTime);

                    break;
                case GameState.Option:

                    if (btnSoundSetting.isClicked == true) currentGameState = GameState.SoundSetting;
                    btnSoundSetting.Update(mouse);
                    
                    if (btnControl.isClicked == true) currentGameState = GameState.ControlSetting;
                    btnControl.Update(mouse);
                    
                    if (btnCredit.isClicked == true) currentGameState = GameState.Credit;
                    btnCredit.Update(mouse);
                    
                    if (btnBackM.isClicked == true) currentGameState = GameState.MainMenu;
                    btnBackM.Update(mouse);
                    break;
                case GameState.Exit:
                    Game.Exit();
                    break;

                // Mục trong Option
                case GameState.SoundSetting:

                    if (btnBackO.isClicked == true) currentGameState = GameState.Option;
                    btnBackO.Update(mouse);

                    break;
                case GameState.ControlSetting:

                    if (btnBackO.isClicked == true) currentGameState = GameState.Option;
                    btnBackO.Update(mouse);

                    break;
                case GameState.Credit:

                    if (btnBackO.isClicked == true) currentGameState = GameState.Option;
                    btnBackO.Update(mouse);

                    break;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            switch (currentGameState)
            {
                // Mục trong Main Menu
                case GameState.MainMenu:

                    // Hiển thị
                   
                    spriteBatch.DrawString(Font, "Main Menu", new Vector2(100, 100), Color.Snow);
                    //-------------------

                    btnNewGame.Draw(spriteBatch);
                    btnContinue.Draw(spriteBatch);
                    btnOption.Draw(spriteBatch);
                    btnExit.Draw(spriteBatch);

                    break;
                case GameState.Playing:

                    // Hiển thị
                    spriteBatch.DrawString(Font, "Playing", new Vector2(100, 100), Color.Black);
                    //-------------------

                    break;
                case GameState.Option:

                    // Hiển thị
                    spriteBatch.DrawString(Font, "Option", new Vector2(100, 100), Color.Black);
                    //-------------------

                    btnSoundSetting.Draw(spriteBatch);
                    btnControl.Draw(spriteBatch);
                    btnCredit.Draw(spriteBatch);
                    btnBackM.Draw(spriteBatch);

                    break;

                // Mục trong Option
                case GameState.SoundSetting:

                    // Hiển thị
                    spriteBatch.DrawString(Font, "Sound Setting", new Vector2(100, 100), Color.Black);
                    //-------------------

                    btnBackO.Draw(spriteBatch);

                    break;
                case GameState.ControlSetting:

                    // Hiển thị
                    spriteBatch.DrawString(Font, "Control Setting", new Vector2(100, 100), Color.Black);
                    //-------------------

                    btnBackO.Draw(spriteBatch);

                    break;
                case GameState.Credit:

                    // Hiển thị
                    spriteBatch.DrawString(Font, "Credit", new Vector2(100, 100), Color.Black);
                    //-------------------

                    btnBackO.Draw(spriteBatch);

                    break;
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
